//
//  YLMemberPrivilegeViewModel.m
//  YLFinanceService
//
//  Created by she1323 on 2017/9/12.
//  Copyright © 2017年 she1323. All rights reserved.
//

#import "YLMemberPrivilegeViewModel.h"
#import "WHNetworkTool.h"
#import "YLAccountTool.h"
#import "WHProgressHUD.h"
#import "YLMemberPrivilegeDetailGroup.h"
#import <YYModel.h>
#import "YLMemberPrivilegeDetailModel.h"
@implementation YLMemberPrivilegeViewModel



- (instancetype)initWithPrivilegeId:(NSInteger)privilegeId
{
    if (self = [super init]) {
        [self setUpWithPrivilegeId:privilegeId];
    }
    return self;
}



- (void)setUpWithPrivilegeId: (NSInteger)privilegeId
{
    
    __weak typeof(self) weakSelf = self;
    _privilegeDetailCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
        return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            
            NSDictionary *param = @{@"PrivilegeID" : @(privilegeId)};
            
            NSString *url = [NSString stringWithFormat:@"%@AppCommon/PGetCusotmerPrivilege", YLURL];
            
            [[WHNetworkTool shareInstance] sendPostWithUrl:url header:[YLAccountTool header] params:param success:^(id responseObject) {
                
                if ([responseObject[@"httpCode"] integerValue] == 200) {
                    
                    
                    
                    YLMemberPrivilegeDetailGroup *group = [YLMemberPrivilegeDetailGroup yy_modelWithDictionary:responseObject[@"httpResult"]];
                    
                    
                    if (group.item.count > 0) {
                        YLMemberPrivilegeDetailModel *detailModel = group.item.firstObject;
                        weakSelf.privilegeImg = detailModel.privilegeImg;
                        
                        {
                            YLMemberPrivilegeModel *model = [[YLMemberPrivilegeModel alloc] init];
                            model.title = @"会员特权";
                            model.content = detailModel.privilegeIntroduce;
                            if (detailModel.isOwn == YES) {
                                model.icon = @"privilegeIntroductionOk";
                            } else {
                                model.icon = @"privilegeIntroductionNo";
                            }
                            [weakSelf.models addObject:model];
                        }
                        
                        {
                            YLMemberPrivilegeModel *model = [[YLMemberPrivilegeModel alloc] init];
                            model.title = @"开放范围";
                            
                            NSMutableString *string = [[NSMutableString alloc] init];
                            for (NSString *str in detailModel.ownLevels) {
                                [string appendFormat:@"%@,", str];
                            }

                            
                            model.content = string;
                            if (detailModel.isOwn == YES) {
                                model.icon = @"openRangeOk";
                            } else {
                                model.icon = @"openRangeNo";
                            }
                            [weakSelf.models addObject:model];
                        }
                        
                    }
                    
                    
                    [subscriber sendNext:nil];
                    
                } else {
                    [WHProgressHUD showError:responseObject[@"message"]];
                }
                [subscriber sendCompleted];
                
            } fail:^(NSError *error) {
                [subscriber sendCompleted];
            }];
            
            return nil;
        }];
    }];
}

- (NSMutableArray<YLMemberPrivilegeModel *> *)models
{
    if (!_models) {
        _models = [NSMutableArray arrayWithCapacity:2];
    }
    return _models;
}

- (void)dealloc
{
    [[WHNetworkTool shareInstance] cancel];
    [WHProgressHUD dismiss];
}




@end
